package pers.karl.mvc.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.karl.mvc.module.user.entity.User;
import pers.karl.mvc.module.user.mapper.UserMapper;
import pers.karl.mvc.module.user.service.UserService;
import pers.karl.mvc.common.util.algorithm.SnowflakeIdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User createUser(User user) {
        user.setUserId(SnowflakeIdWorker.getId());
        user.setCreatedTime(new Date());
        user.setCreatedBy(0L);

        int count = userMapper.insert(user);

        return count > 0 ? user : null;
    }

    @Override
    public List<User> listUser() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(Wrapper -> Wrapper.eq( User::getUserName, "张三").or().eq(User::getUserName, "李四"));

        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<Map<String, Object>> pageUser(Long currentPage, Long pageSize) {
        Page<User> page = new Page<>(currentPage == null ? 0 : currentPage, pageSize == null ? 10 : pageSize);
        return userMapper.selectUserPage(page);
    }

}
